package main

import (
	"fmt"
	"greatTool/db"

	"github.com/spf13/cobra"
)

var migrateCmd = &cobra.Command{
	Use:   "migrate [source] [target]",
	Short: "数据迁移工具",
	Long: `将数据从一个存储迁移到另一个存储
支持迁移方向: local->cloud, cloud->local, local->local

示例:
  ttl migrate local cloud    # 从本地迁移到云端
  ttl migrate cloud local    # 从云端迁移到本地`,
	Args: cobra.ExactArgs(2),
	RunE: func(cmd *cobra.Command, args []string) error {
		sourceType := args[0]
		targetType := args[1]

		// 验证迁移方向
		if sourceType != "local" && sourceType != "cloud" {
			return fmt.Errorf("源存储类型必须是 local 或 cloud")
		}
		if targetType != "local" && targetType != "cloud" {
			return fmt.Errorf("目标存储类型必须是 local 或 cloud")
		}
		if sourceType == targetType {
			return fmt.Errorf("源和目标存储类型不能相同")
		}

		// 获取源存储配置（如果是云存储）
		var sourceAPIURL, sourceAPIKey string
		var sourceTimeout int

		if sourceType == "cloud" {
			sourceAPIURL, _ = cmd.Flags().GetString("source-url")
			sourceAPIKey, _ = cmd.Flags().GetString("source-key")
			sourceTimeout, _ = cmd.Flags().GetInt("source-timeout")

			if sourceAPIURL == "" || sourceAPIKey == "" {
				return fmt.Errorf("源云存储需要提供 --source-url 和 --source-key 参数")
			}
		}

		return db.MigrateData(
			sourceType, targetType,
			sourceAPIURL, sourceAPIKey, sourceTimeout,
			cloudAPIURL, cloudAPIKey, cloudTimeout,
			debug)
	},
}

func init() {
	// 添加迁移命令特有的标志
	migrateCmd.Flags().String("source-url", "", "源云存储API地址")
	migrateCmd.Flags().String("source-key", "", "源云存储API密钥")
	migrateCmd.Flags().Int("source-timeout", 30, "源云存储超时时间(秒)")
}
